package com.me.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.me.entity.Admin;
import com.me.entity.StudentPaper;
import com.me.service.AdminService;
import com.me.service.ConfigService;
import com.me.service.StudentPaperService;

@Controller
@RequestMapping("/login")
@SessionAttributes({"adminUser","student","totalTime"})
public class LoginController extends BaseController {
	/**
	 * 注入业务组件
	 */
	@Resource
	private AdminService adminService;
	@Resource
	private StudentPaperService studentPaperService;
	@Resource
	private ConfigService configService;
	
	/**
	 * 进入登录页
	 */
	@RequestMapping("/toLogin.do")
	public String toLogin(HttpSession session){
		session.invalidate();
		return "login";
	}
	
	/**
	 * 登出操作
	 */
	@RequestMapping("/logout.do")
	@ResponseBody
	public Map<String, Object> login(HttpSession session){
		session.invalidate();
		return messageSuccess();
	}
	
	/**
	 * 登录操作
	 */
	@RequestMapping("/signUp.do")
	@ResponseBody
	public Map<String, Object> login(Admin admin, StudentPaper studentPaper, Model model, HttpSession session){
		LOG.info("------------管理员登录：" + admin + "--------------");
		LOG.info("------------学生登录：" + studentPaper + "--------------");
		try {
			// 如果还没注册，则去注册
			if(!adminService.isExistAdmin(admin)){
				if(!studentPaperService.isExistStudent(studentPaper)){
					return messageFailure(0, "该账号尚未注册，现在去注册？");
				} else{
					// 如果学生用户名或密码错误，则弹出对话框
					if(!studentPaperService.checkLogin(studentPaper)){
						return messageFailure(1, "密码错误！");
					}
					Integer paperState = studentPaperService.getPaperStatus(studentPaper);
					// 如果试卷状态为3,则不允许登录
					if(paperState == 3){
						return messageFailure(2, "您已完成考试，不能再次考试！");
					}
					// 如果试卷状态为1或4,则说明考生在考试中意外退出，也不允许登录
					if(paperState == 1 || paperState == 4){
						// 修改试卷状态为 4
						studentPaper = studentPaperService.queryByAccount(studentPaper);
						studentPaper.setPaperState(4);
						studentPaperService.update(studentPaper);
						return messageFailure(2, "您在考试中途意外退出，请联系教师进行重考！");
					}
					// 如果登录成功，则跳转到考试主页
					StudentPaper studentPaperSession = (StudentPaper) session.getAttribute("student");
					LOG.info("------------原Session中的试卷信息：" + studentPaperSession + "--------------");
					// 如果session中没有该试卷信息，或该试卷信息与session中的不是同一个考生
					if(studentPaperSession == null || (studentPaperSession != null && !studentPaperSession.getAccount().equals(studentPaper.getAccount()))){
						studentPaper = studentPaperService.queryByAccount(studentPaper);
						model.addAttribute("totalTime", configService.getTotalTime());
						model.addAttribute("student", studentPaper);
					}
					LOG.info("------------现Session中的试卷信息：" + studentPaper + "--------------");
					return messageSuccess("student");
				}
			}
			// 如果管理员用户名或密码错误，则弹出对话框
			if(!adminService.checkLogin(admin)){
				return messageFailure(1, "密码错误！");
			}
			// 如果登录成功，则跳转到管理员主页
			model.addAttribute("adminUser", admin);
			return messageSuccess("admin");
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "登录失败！");
		}
	}
	
	/**
	 * 跳转到系统后台主页
	 */
	@RequestMapping("/adminIndex.do")
	public String toaIndex(){
		return "admin/index";
	}
	
	/**
	 * 跳转到系统考试主页
	 */
	@RequestMapping("/studentIndex.do")
	public String tosIndex(){
		return "student/index";
	}
}
